package ToYesterday;

import org.junit.Test;

public class Offer_50_p {

    /*
     * 定义一个函数，输入两个字符串，从第一个字符串中删除在第二个字符串中出现过的所有字符.
     * 例如从第一个字符“We are students. "中删除在第二个字符串'aeiou'中出现过的字符得到的结果是”W r stdnts. “
     *
     *
     * */
    @Test
    public void TestP01() {
        String str1 = "We are students. ";
        String str2 = "aeiou";
        System.out.println(SolutionP01(str1, str2));

    }

    public String SolutionP01(String str1, String str2) {
        StringBuilder builder = new StringBuilder();
        char[] chars1 = str1.toCharArray();
        char[] chars2 = str2.toCharArray();
        boolean[] listchars1 = new boolean[256];
        for (char c : chars2) {
            listchars1[c] = true;
        }
        for (char c : chars1) {
            if (listchars1[c] == false) {
                builder.append(c);
            }
        }
        return new String(builder);
    }


    /*
     * 定义一个函数，删除字符串中所有重复出现的字符。例如输入"google"，删除重复的富足之后的结果是"gole"。
     * */
    @Test
    public void Test02() {
        String str = "google";
        System.out.println(SolutionP02(str));
    }

    public String SolutionP02(String str) {
        char[] chars = str.toCharArray();
        boolean[] strint = new boolean[26];
        StringBuilder builder = new StringBuilder();
        for (char c : chars) {
            if (strint[c - 'a']) {
                continue;
            }
            strint[c - 'a'] = true;
            builder.append(c);

        }
        return new String(builder);
    }

    /*
     * 在英语中，如果两个单词中出现的字母相同，并且每个字母出现的次数也相同，那么这两个单词互为变位词。例如silent,listen | evil,live。
     * 完成一个函数，判断输入的两个字符串是不是互为变位词。
     * */
    @Test
    public void Test03() {
        String str1 = "live";
        String str2 = "evil";
        System.out.println(SolutionP03(str1, str2));
    }

    public boolean SolutionP03(String str1, String str2) {
        char[] chars = str1.toCharArray();
        char[] chars2 = str2.toCharArray();
        boolean[] booleans = new boolean[52];
        for (char c : chars){
            booleans[c-'a'] = true;
        }
        for (char c: chars2){
            if (!booleans[c - 'a'])return false;
        }
        return true;
    }

}
